//
//  SNDownloadPhotoController.m
//  GCDTest
//
//  Created by Sunny on 21/6/18.
//  Copyright © 2018年 Temporary. All rights reserved.
//

#import "SNDownloadPhotoController.h"

@interface SNDownloadPhotoController ()

@end

@implementation SNDownloadPhotoController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    // Do any additional setup after loading the view, typically from a nib.
    
    [self.view addSubview:self.btn];
    
}

-(UIActivityIndicatorView *)indicator {
    
    if (_indicator == nil) {
        
        _indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
        
        _indicator.frame = CGRectMake(0, 60, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.width);
        
        _indicator.backgroundColor = [UIColor lightGrayColor];
        
    }
    
    [self.view addSubview:_indicator];
    
    return _indicator;
    
}



-(UIImageView *)imgV {
    
    if (_imgV == nil) {
        
        _imgV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 230, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.width)];
        
        _imgV.backgroundColor = [UIColor blueColor];
        
    }
    
    [self.view addSubview:_imgV];
    
    return _imgV;
    
}



-(UIButton *)btn {
    
    if (_btn == nil) {
        
        _btn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        _btn.frame = CGRectMake(10, 110, 200, 50);
        
        _btn.backgroundColor = [UIColor redColor];
        
        [_btn setTitle:@"点击加载图片" forState:UIControlStateNormal];
        
        [_btn addTarget:self action:@selector(loadImage) forControlEvents:UIControlEventTouchUpInside];
        
    }
    
    return _btn;
    
}

/** 加载图片 */

-(void)loadImage {
    
    
    self.indicator.hidden = NO;
    
    [self.indicator startAnimating];
    
    [self.imgV addSubview:self.indicator];
    
    
    // 开启一个线程进行数据请求
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        NSURL *url1 = [NSURL URLWithString:@"http://pic.qiantucdn.com/58pic/18/32/08/32658PICPUs_1024.png"];
        
        NSError *error;
        
        NSData *data = [NSData dataWithContentsOfURL:url1];
        
        if (data != nil) {
            
            // 在主线程中完成UI操作
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                [self.indicator stopAnimating];
                
                self.indicator.hidden = YES;
                
                UIImage *image = [UIImage imageWithData:data];
                
                self.imgV.image = image;
                
            });
            
        }else {
            
            NSLog(@"error whin download:%@",error);
            
        }
        
    });
    
}

- (void)didReceiveMemoryWarning {
    
    [super didReceiveMemoryWarning];
    
    // Dispose of any resources that can be recreated.
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
